package cn.hxzy.course.controller;



import cn.hxzy.common.domain.Course;
import cn.hxzy.common.domain.Courseware;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;

import cn.hxzy.course.service.CourseService;
import cn.hxzy.course.service.CoursewareService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author niuyi
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/courseware")
public class CoursewareController {

    @Autowired
    private CoursewareService coursewareService;
    @Autowired
    private CourseService courseService;


    //根据小节编号查询课件
    @GetMapping("/selectWareList")
    public R<List<Courseware>> selectWareList(@RequestParam("sectionId") Integer sectionId) {

        List<Courseware> coursewares = coursewareService.selectWareList(sectionId);
            return R.success(coursewares);

    }
    //新增小节的课件
    @PostMapping("/saveCourseware")
    public R saveVideo(@RequestBody Courseware courseware) {
        boolean save = coursewareService.save(courseware);
        if (save){
            return R.success(save);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }
    /**
     * 搜索框搜索课件
     * @return
     */
    @GetMapping("/selectWare/{coursewareName}/{courseId}")
    public R<List<Courseware>> selectWare(@PathVariable("coursewareName") String coursewareName,@PathVariable("courseId") Integer courseId) {
        List<Courseware> coursewares = coursewareService.selectCourseware(coursewareName,courseId);
        return R.success(coursewares);
    }

    @GetMapping("/getCoursewareNum")
    public R getVideoNum(@RequestParam("courseId") Long courseId) {
        return R.success(coursewareService.selectCount(courseId));
    }


    //删除课件
    @DeleteMapping("/deleteCourseware")
    public R deleteCourseware(@RequestParam("coursewareId") Integer coursewareId) {
        Integer i = coursewareService.deleteCourseware(coursewareId);
        if (i>0){
            return R.success("删除成功");
        }
        return R.error(401,"删除失败");
    }
    //根据课程ID修改内容
    @RequestMapping("/insertuser")
    public R insertuser(@RequestBody Course course){
        int reviseurse = courseService.reviseurse(course);
        if(reviseurse>0){
            return R.success("修改成功");
        }
        return R.error(401,"修改失败");
    }
}

